package com.lesson4.factory.pizzastore.order;

import com.lesson4.factory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/8/27 11:42
 */
public class OrderPizza
{
//    public OrderPizza()
//    {
//        Pizza pizza = null;
//        String orderType;
//        do{
//            orderType = getType();
//            if(orderType.equals("greek")){
//                pizza = new GreekPizza();
//                pizza.setName("希腊披萨");
//            } else if(orderType.equals("cheese")){
//                pizza = new CheesePizza();
//                pizza.setName("奶酪披萨");
//            } else {
//                break;
//            }
//            pizza.prepare();
//            pizza.bake();
//            pizza.cut();
//            pizza.box();
//        }while (true);
//    }

    public OrderPizza(SimpleFactory simpleFactory)
    {
        setSimpleFactory(simpleFactory);
    }

    SimpleFactory simpleFactory;
    Pizza pizza = null;

    public void setSimpleFactory(SimpleFactory simpleFactory)
    {
        String orderType = "";
        this.simpleFactory = simpleFactory;
        do
        {
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);
            if (pizza != null)
            {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else
            {
                System.out.println("订购失败！");
            }
        } while (true);
    }

    private String getType()
    {
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza 种类：");
            String line = reader.readLine();
            return line;
        } catch (IOException e)
        {
            e.printStackTrace();
            return "";
        }
    }
}
